home *** CD-ROM | disk | FTP | other *** search
- Path: news.cern.ch!danpop
- From: danpop@mail.cern.ch (Dan Pop)
- Newsgroups: comp.lang.c
- Subject: Re: What is wrong with this loop?
- Date: 19 Apr 96 20:09:05 GMT
- Organization: CERN European Lab for Particle Physics
- Message-ID: <danpop.829944545@news.cern.ch>
- References: <4l86la$1t9@uwm.edu> <4l8lt7$d9h@HOPPER.ACM.ORG>
- NNTP-Posting-Host: ues5.cern.ch
- Mime-Version: 1.0
- Content-Type: text/plain; charset=US-ASCII
- Content-Transfer-Encoding: 7bit
- X-Newsreader: NN version 6.5.0 #18 (NOV)
-
- In <4l8lt7$d9h@HOPPER.ACM.ORG> varnk@e62.diebold.com (Ken Varn) writes:
-
- >In article <4l86la$1t9@uwm.edu>, mardavuy@alpha2.csd.uwm.edu says...
- >/>
- >/>#include <stdio.h>
- >/>int main(void)
- >/>{
- >/> int dia;
- >/>
- >/> char cd;
- >/>
- >/> ...
- >/>
- >/> scanf("%c", &cd);
- >/> while (cd != 'm' || cd ! 'f' || cd != 'o')
- >/> {
- >/> printf("Re-enter m, f, or o.\n");
- >/> scanf(%c", &cd);
- >/> }
- >/> ...
- >/>}
- >/>
- >/>It works fine, except that it gives me the printf() twice.
- >
- >
- >Try changing your scanf() as follows:
- >
- >scanf("%c ",&cd);
-
- Did you actually try your advice? Obviously not!
-
- >This tells scanf() to read past any white space characters after the
- >charadter read.
-
- And this is why your solution is brain dead. scanf cannot know that it
- has read all the whitespace _before_ reading a non-whitespace character.
- Which means that the scanf call will block until the user types _two_
- lines of text, which is definitely not what the original poster wanted or
- what the user of the program would expect.
-
- Dan
- --
- Dan Pop
- CERN, CN Division
- Email: danpop@mail.cern.ch
- Mail: CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
-